package com.fortune.rainshineservice.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.fortune.rainshinecommon.base.MsgModel;
import com.fortune.rainshinecommon.enums.ErrorCodes;
import com.fortune.rainshinecommon.enums.ResultCode;
import com.fortune.rainshinecommon.model.CustomerModel;
import com.fortune.rainshinecommon.model.ShopModel;
import com.fortune.rainshinecommon.model.system.CardModel;
import com.fortune.rainshinecommon.model.system.CustomerServiceRecordModel;
import com.fortune.rainshinecommon.model.system.User;
import com.fortune.rainshinecommon.query.ServiceQuery;
import com.fortune.rainshinecommon.util.DateUtils;
import com.fortune.rainshineservice.mapper.*;
import com.fortune.rainshineservice.service.IServerService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @program: rainShine
 * @description: 服务实现类
 * @author: tangyn
 * @create: 2019-05-14
 **/
@Service
public class ServerServiceImpl implements IServerService {
    private static final Logger logger = LoggerFactory.getLogger(ServerServiceImpl.class);

    private final ShopMapper shopMapper;

    private final SysUserMapper sysUserMapper;

    private final CardMapper cardMapper;

    private final CustomerMapper customerMapper;

    private final CustomerServiceRecordMapper customerServiceRecordMapper;

    @Autowired
    public ServerServiceImpl(ShopMapper shopMapper, SysUserMapper sysUserMapper, CardMapper cardMapper, CustomerMapper customerMapper, CustomerServiceRecordMapper customerServiceRecordMapper) {
        this.shopMapper = shopMapper;
        this.sysUserMapper = sysUserMapper;
        this.cardMapper = cardMapper;
        this.customerMapper = customerMapper;
        this.customerServiceRecordMapper = customerServiceRecordMapper;
    }

    @Override
    public MsgModel getServiceList(ServiceQuery query) {
        try {
            Wrapper<CustomerServiceRecordModel> wrapper = new EntityWrapper<>();
            wrapper.eq("shop_id", query.getShopId());
            if(query.getServerUID() != null && query.getServerUID() > 0){
                wrapper.eq("create_id", query.getServerUID());
            }
            if(StringUtils.isNotBlank(query.getBeginTime()) || StringUtils.isNotBlank(query.getEndTime())){
                String today = DateUtils.format(new Date(),DateUtils.YYYY_MMM_DD);
                if(StringUtils.isNotBlank(query.getBeginTime()) && StringUtils.isNotBlank(query.getEndTime())){
                    if(!DateUtils.compareDate(query.getEndTime(), query.getBeginTime())){
                        return new MsgModel("开始日期不能大于结束日期！", ResultCode.FAILURE);
                    }
                    wrapper.and().between("create_time",query.getBeginTime()+" 00:00:00",query.getEndTime()+" 23:59:59");
                }else{
                    if(StringUtils.isNotBlank(query.getBeginTime())){
                        if(!DateUtils.compareDate(today, query.getBeginTime())){
                            return new MsgModel("开始日期不能大于当前日期！", ResultCode.FAILURE);
                        }
                        wrapper.and().between("create_time",query.getBeginTime()+" 00:00:00",today+" 23:59:59");
                    }else{
                        wrapper.and().between("create_time", query.getEndTime()+" 00:00:00",query.getEndTime()+" 23:59:59");
                    }
                }
            }
            Page<CustomerServiceRecordModel> page = new Page<>(query.getPage(), query.getLimit());
            List<CustomerServiceRecordModel> list = customerServiceRecordMapper.selectPage(page, wrapper);
            if(!list.isEmpty()){
                list.forEach(e -> {
                    if(e.getShopId() != null){
                        ShopModel se = shopMapper.selectById(e.getShopId());
                        e.setShopName(se.getShopName());
                    }
                    User serviceUser = sysUserMapper.selectByUserId(e.getCreateId());
                    e.setServiceUserName(serviceUser.getNickName());
                    CardModel cardModel = cardMapper.selectById(e.getCardId());
                    if(cardModel != null){
                        e.setTypeName(cardModel.getServiceName());
                    }
                    CustomerModel customerModel = customerMapper.selectById(e.getCustomerId());
                    e.setCustomerName(customerModel!=null?customerModel.getCustomerName():"");
                });
            }
            int total = customerServiceRecordMapper.selectCount(wrapper);
            return new MsgModel(total, list);
        }catch (Exception e){
            logger.error(ErrorCodes.BUSINESS_EXCEPTION.getMessage(), e);
            return new MsgModel("获取服务列表失败！", ResultCode.FAILURE);
        }
    }
}
